package cn.dream.ebag.MessengerLib;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * Created by qktang on 2018/6/27 in EBag.
 * cn.dream.ebagcomlib.helper
 */
public abstract class WeakReferenceHandler<T> extends Handler {

    private WeakReference<T> weakReference;


    public WeakReferenceHandler(T weakReference) {
        this.weakReference = new WeakReference<T>(weakReference);

    }

    public WeakReferenceHandler(T weakReference, Looper looper) {
        super(looper);
        this.weakReference = new WeakReference<T>(weakReference);
    }

    public WeakReferenceHandler(T weakReference, Looper looper, Callback callback) {
        super(looper, callback);
        this.weakReference = new WeakReference<T>(weakReference);
    }

    @Override
    public void handleMessage(Message msg) {
        final T handlerInstance = weakReference.get();
        if (handlerInstance != null) {
            handleMessage(handlerInstance, msg);
        }
    }

    public abstract void handleMessage(T instance, Message msg);
}
